package com.shibaobei.activity.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Administrator on 2020/4/23 0023.
 * 历史数据
 */
@RestController
@RequestMapping("/history")
public class HistoryController {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;

    /**
     * 发布执行流程
     */
    @RequestMapping("/firstDemo")
    public void firstDemo() {

        //根据bpmn文件部署流程
        Deployment deployment = repositoryService
                .createDeployment()
                .addClasspathResource("processes/demo.bpmn")
                .deploy();
        //获取流程定义
        ProcessDefinition processDefinition = repositoryService
                .createProcessDefinitionQuery()
                .deploymentId(deployment.getId())
                .singleResult();
        //启动流程定义，返回流程实例
        ProcessInstance pi = runtimeService
                .startProcessInstanceById(processDefinition.getId());
        String processId = pi.getId();
        System.out.println("流程创建成功，当前流程实例ID："+processId);

        Task task=taskService
                .createTaskQuery()
                .processInstanceId(processId)
                .singleResult();
        System.out.println("第一次执行前："+task);
        taskService.complete(task.getId());

        task = taskService
                .createTaskQuery()
                .processInstanceId(processId)
                .singleResult();
        System.out.println("第二次执行前："+task);
        taskService.complete(task.getId());

        task = taskService
                .createTaskQuery()
                .processInstanceId(processId)
                .singleResult();
        System.out.println("第三次执行前："+task);

        task = taskService
                .createTaskQuery()
                .processInstanceId(processId)
                .singleResult();
        System.out.println("task为null，任务执行完毕："+task);
    }

    /**
     * 查询任务列表
     * @param taskService
     */
    @RequestMapping("/taskList")
    @ResponseBody
    public  JSONArray taslList(TaskService taskService){
        TaskQuery taskQuery = taskService.createTaskQuery();
        List<Task> list = taskQuery.list();
        JSONArray array = new JSONArray();
        JSONObject json = null;
        for (Task task:list){
            json = new JSONObject();
            json.put(task.getName(),JSONObject.toJSON(task));
            array.add(json);
        }
        return array;
    }

    /**
     * 历史流程
     */
    @RequestMapping("/historyProcess")
    @ResponseBody
    public JSONArray historyProcess(){
        HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery();
        List<HistoricProcessInstance> list = query.list();
        JSONArray array = new JSONArray();
        JSONObject json = null;
        for(HistoricProcessInstance historiy:list){
            json = new JSONObject();
            json.put(historiy.getId(),JSONObject.toJSON(historiy));
            array.add(json);
        }
        return array;
    }

    /**
     * 历史活动数据列表
     * @return
     */
    @RequestMapping("/historyActivity")
    @ResponseBody
    public JSONArray historyActivity(){
        HistoricActivityInstanceQuery activity = historyService.createHistoricActivityInstanceQuery();
        activity.orderByProcessDefinitionId().desc();
        activity.orderByHistoricActivityInstanceEndTime().asc();
        List<HistoricActivityInstance> list = activity.list();
        JSONArray array = new JSONArray();
        JSONObject json = null;
        for(HistoricActivityInstance historiy:list){
            json = new JSONObject();
            json.put(historiy.getId(),JSONObject.toJSON(historiy));
            array.add(json);
        }
        return array;
    }

    /**
     * 历史任务数据列表
     * @return
     */
    @RequestMapping("/historyTask")
    @ResponseBody
    public JSONArray historyTask(){
        HistoricTaskInstanceQuery historicTask = historyService.createHistoricTaskInstanceQuery();
        historicTask.orderByProcessInstanceId().asc();
        historicTask.orderByHistoricTaskInstanceEndTime().desc();
        List<HistoricTaskInstance> list = historicTask.list();
        JSONArray array = new JSONArray();
        JSONObject json = null;
        for(HistoricTaskInstance task:list){
            json = new JSONObject();
            json.put(task.getId(),JSONObject.toJSON(task));
            array.add(json);
        }
        return array;
    }

}
